package com.richard.base.starter

import android.content.Context
import android.os.Environment
import com.facebook.cache.disk.DiskCacheConfig
import com.facebook.common.internal.Supplier
import com.facebook.common.util.ByteConstants
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.imagepipeline.cache.MemoryCacheParams
import com.facebook.imagepipeline.core.ImagePipelineConfig
import com.richard.base.BaseApplication
import com.richard.base.util.FileUtil
import com.wxy.appstartfaster.executor.TaskExceutorManager
import com.wxy.appstartfaster.task.AppStartTask
import java.io.File
import java.util.concurrent.Executor


/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/7/27     8:24 PM
 * 用途:
 ***************************************
 */
class InitSdkToolTask: AppStartTask() {
    private val MAX_MEM = 30 * ByteConstants.MB

    override fun run() {
        initFresco()
    }

    override fun isRunOnMainThread(): Boolean {
        return false
    }


    override fun runOnExecutor(): Executor {
        return TaskExceutorManager.getInstance().cpuThreadPoolExecutor
    }
    /**
     * 初始化图片图并设置带有包名的资源路径
     */
    private fun initFresco() {


        Fresco.initialize(BaseApplication.instance, getConfigureCaches(BaseApplication.instance))
//        Fresco.initialize(
//            context,
//            FrescoImagePipelineConfigFactory.getOkHttpImagePipelineConfig(context)
//        )
    }

    override fun getDependsTaskList(): MutableList<Class<out AppStartTask>> {//需要依赖arouter 和 file的Task
        return mutableListOf(InitArouterTask::class.java, InitFileTask::class.java)
    }


    private fun getConfigureCaches(context: Context): ImagePipelineConfig? {
        val bitmapCacheParams = MemoryCacheParams( // 内存缓存中总图片的最大大小,以字节为单位。
            MAX_MEM, Int.MAX_VALUE,  // 内存缓存中准备清除但尚未被删除的总图片的最大大小,以字节为单位。
            MAX_MEM, Int.MAX_VALUE, Int.MAX_VALUE
        )
        val mSupplierMemoryCacheParams: Supplier<MemoryCacheParams> = Supplier {
                bitmapCacheParams
        }
        val builder = ImagePipelineConfig.newBuilder(context)
        builder.setBitmapMemoryCacheParamsSupplier(mSupplierMemoryCacheParams)


        val cacheConfig = DiskCacheConfig.newBuilder(BaseApplication.instance)
            .setBaseDirectoryName("fimages")
            .setBaseDirectoryPath(File(FileUtil.rootCachePath))
            .setMaxCacheSize(100L * ByteConstants.MB)
            .build()

        builder.setMainDiskCacheConfig(cacheConfig)
        return builder.build()
    }


}